﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using LaserScan;

namespace DiameterAnalysis
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        IEnumerable<FileInfo> sectionFileList = null;
        SectionSolver sectionSolver = new SectionSolver();
        double deltdiameter;
        private void ReadSection_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog()==DialogResult.OK)
            {
               sectionFileList = new DirectoryInfo(folderBrowserDialog1.SelectedPath).EnumerateFiles();
            }
            deltdiameter =double.Parse(comboBox1.SelectedItem.ToString())/100;
        }

        private void CalculateSection_Click(object sender, EventArgs e)
        {
            foreach (var item in sectionFileList)
            {
                List<double[]> XY = new List<double[]>();
                string filename = Path.GetFileNameWithoutExtension(item.FullName);
                int index = filename.IndexOf('_');
                string ring = filename.Substring(0, index);
                string ringSectionNum = filename.Substring(index + 1);
                if (ringSectionNum=="0")
                {
                    using (StreamReader fread = new StreamReader(item.FullName))
                    {
                        while (!fread.EndOfStream)
                        {
                            string[] readLine = fread.ReadLine().Split(',');
                            double X = double.Parse(readLine[0]);
                            double Y = double.Parse(readLine[2]);
                            XY.Add(new double[2] { X, Y });
                        }
                    }
                    List<double[]> XYEllipseFited = sectionSolver.EllipseFitDenoise(XY);
                    List<double[]> segmentsPara = sectionSolver.SegmentedArcFit(XYEllipseFited, new double[6] { 138, 73, 8, -8, -73, -138 }, new double[3] { 0, 0, 2.75 });
                    double diameter = sectionSolver.GetDiameterNew(segmentsPara, sectionSolver.SectionCenter);
                    if (diameter < 5.4)
                    {
                        continue;
                    }
                    else
                    {
                        double Xc = sectionSolver.SectionCenter[0]; double Yc = sectionSolver.SectionCenter[1];
                        double[] X1 = new double[2] { Math.Sqrt(Math.Pow(segmentsPara[0][2], 2) - Math.Pow(Yc + deltdiameter - segmentsPara[0][1], 2)) + segmentsPara[0][0], Yc + deltdiameter };
                        double[] X2 = new double[2] { -Math.Sqrt(Math.Pow(segmentsPara[4][2], 2) - Math.Pow(Yc + deltdiameter - segmentsPara[4][1], 2)) + segmentsPara[4][0], Yc + deltdiameter };
                        double[] X3 = new double[2] { Math.Sqrt(Math.Pow(segmentsPara[0][2], 2) - Math.Pow(Yc - deltdiameter - segmentsPara[0][1], 2)) + segmentsPara[0][0], Yc - deltdiameter };
                        double[] X4 = new double[2] { -Math.Sqrt(Math.Pow(segmentsPara[4][2], 2) - Math.Pow(Yc - deltdiameter - segmentsPara[4][1], 2)) + segmentsPara[4][0], Yc - deltdiameter };
                        //向上平移10cm直径
                        double diameterUptencm = X1[0] - X2[0];
                        //向下平移10cm直径
                        double diameterDowntencm = X3[0] - X4[0];
                        //负上正下10cm直径
                        double diameterCrosseNegativeK = Math.Sqrt(Math.Pow(deltdiameter * 2, 2) + Math.Pow(X3[0] - X2[0], 2));
                        //负下正上10cm直径
                        double diameterCrossePositiveK = Math.Sqrt(Math.Pow(deltdiameter * 2, 2) + Math.Pow(X1[0] - X4[0], 2));

                        using (StreamWriter fwrite = new StreamWriter(Directory.GetParent(Path.GetDirectoryName(item.FullName)) + "\\result" + (deltdiameter * 100).ToString() + ".csv", true))
                        {
                            fwrite.WriteLine(ring + "," + ringSectionNum + "," + diameter.ToString("0.0000") + "," + diameterUptencm.ToString("0.0000") + ","
                                + diameterDowntencm.ToString("0.0000") + "," + diameterCrosseNegativeK.ToString("0.0000") + "," + diameterCrossePositiveK.ToString("0.0000"));
                        }
                    }
                }
                else
                {
                    continue;
                }
                
            }
        }
    }
}
